package com.sky.service.impl;

import ch.qos.logback.core.status.StatusBase;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //添加setmeal 利用属性复制 得到setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties( setmealDTO,setmeal );
        setmealMapper.insert(setmeal);
        //获取套餐id
        Long setmeal_id = setmeal.getId();
        System.out.println(setmeal_id);
        //添加菜品套餐到setmeal_dish
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach( setmealDish -> {
                //设置套餐id
                setmealDish.setSetmealId( setmeal_id );
            } );
        }
        //批量插入菜品套餐关系
        setmealDishMapper.insertBatch(setmealDishes);
//        setmealDishMapper.insert(setmealDishes);


    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage( setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize() );
        //条件查询
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除套餐
     * @param ids
     */
    //同时删除多个套餐，加入其中有一个在起售中那么就删除失败，菜品套餐关系表和套餐表具有一致性，加入同一事务
    public void deleteBatch(List<Long> ids) {

        //1.判断是否是起售中，若是则不能删除，反之可以删除
        ids.forEach( id->{
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE) {
                throw  new DeletionNotAllowedException( MessageConstant.SETMEAL_ON_SALE );
            }
        } );

        //2.删除对应的菜品套餐关系表中的关系
        ids.forEach( setmealId->{
            setmealMapper.deleteById(setmealId);
            setmealDishMapper.deleteBySetmealId(setmealId);

        } );
    }

    /**
     * 根据ID查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        //获取套餐
        Setmeal setmeal = setmealMapper.getById( id );
        //获取分类名称
//        Long categoryId = setmeal.getCategoryId();
//        Category category =  categoryMapper.getById(categoryId);
//        String categoryName = category.getName();
        //获取套餐中的菜品
        List<SetmealDish> setmealDish = setmealDishMapper.getDishBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties( setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
//        setmealVO.setCategoryName( categoryName );
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties( setmealDTO,setmeal );
        //1.修改套餐表
        setmealMapper.update( setmeal );
        //2.修改套餐菜品表 = 先删除套餐菜品表 + 再添加套餐菜品表
        //删除套餐和菜品关系
        setmealDishMapper.deleteBySetmealId( setmealDTO.getId() );
        //设置setmealId
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach( setmealDish -> {
            setmealDish.setSetmealId( setmealDTO.getId() );
        } );
        //3.重新插入套餐和菜品关系
        setmealDishMapper.insertBatch( setmealDishes );
    }

    /**
     * 停售起售套餐
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        //套餐中有停售的菜品就不能起售套餐
        if(status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            dishList.forEach( dish -> {
                if(dish.getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            } );

        }
//        Setmeal setmeal = new Setmeal();
//        setmeal.setStatus( status );
//        setmeal.setId( id );
        Setmeal setmeal = Setmeal.builder()
                .status( status )
                .id( id )
                .build();
        setmealMapper.update( setmeal );
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> getByCategoryId(Integer categoryId) {
        List<Setmeal> setmeal =  setmealMapper.getBYCategoryId(categoryId);
        return setmeal;
    }

    /**
     * 根据套餐id查询菜品
     * @param id
     * @return
     */
    public List<DishItemVO> getDishsById(Long id) {
        //select * from

        return setmealMapper.getDishBySetmealId(id);
    }
}
